import { _decorator, Component, Label, Node, UITransform, sys } from 'cc';
import { MatchRanking } from '../../MatchRanking/MatchRanking';
import { WsMgr } from '../../TCP/WsMgr';
import { RankingResult } from '../../Cmd/RankingResult';
import { GameResult } from '../../Cmd/GameResult';
import { NetDefine } from '../../Cmd/NetDefine';
import { DiceCupController } from './DiceCupController';
import { GameList } from '../../GameList/GameList';
import AudioMgr from '../../Common/AudioMgr';
import HelloPlayer from '../../HelloPlayer';
import { ControlSVGA } from './ControlSVGA';
const { ccclass, property } = _decorator;

@ccclass('DiceGame')
export class DiceGame extends Component {

    @property({ type: Label })
    rankingLable: Label = null;

    @property({ type: Label })
    playingLable: Label = null;

    
    @property({ type: Label })
    totalLable: Label = null;

    @property({ type: Node ,tooltip: "排行榜root"  })
    RankingRootNode: Node = null;
    
    @property({ type: Node ,tooltip: "排行榜"  })
    RankingNode: Node = null;

    @property({ type: Node })
    matchRankingNode: Node = null;

    @property({ type: Node })
    gameListNode: Node = null;

    @property({ type: Node })
    gameDesc: Node = null;

   
    @property({ type: Node ,tooltip: "底部按钮根节点"})
    BtnRootNode: Node = null;

    private wsSrc: WsMgr;
    private matchRanking:MatchRanking;
    private diceCupController:DiceCupController;
    private gameList:GameList;
    private audioSrc:AudioMgr;
    private svgaCtrl:ControlSVGA;
    private firstName='isFristClose-10006';

  start() {
    // 获取Canvas节点
    const canvas = this.node;
    
    // 计算距离顶部50像素的位置
    const screenHeight = canvas.getComponent(UITransform).height;
    const targetY = screenHeight / 2 - 142;
    
    // 设置节点位置
    this.RankingRootNode.setPosition(0, targetY, 0);
   

    const targetY2 = -(screenHeight / 2 - 100);
    // 设置节点位置
    this.BtnRootNode.setPosition(0, targetY2, 0);

   
     const isFristClose=  sys.localStorage.getItem(this.firstName);
     if (isFristClose==null) {
        this.show(this.gameDesc) ;
         console.log('fristShow',isFristClose);
     }
    
   
}

    onLoad() {
        this.wsSrc = WsMgr.getInstance();
        this.wsSrc.initGameMgr(this);
        this.matchRanking=this.getComponent(MatchRanking);
        this.diceCupController=this.getComponentInChildren(DiceCupController);
        let ranking= this.wsSrc.mRankingResult; 
        this.updateMatchRanking(ranking);
        this.gameList=this.getComponent(GameList);
        this.audioSrc = this.getComponent(AudioMgr);
        this.svgaCtrl = this.getComponent(ControlSVGA);

        this.diceCupController.initAudioMgr(this.audioSrc);
        let info=this.wsSrc.getPlayerInfo()
        if (info!=null) {
        this.diceCupController.setTotalScore(info.score);
        }
     
    }

    private show(nd: Node): void {
        nd.active = true;
    }

    private hide(nd: Node): void {
        nd.active = false;
    }

     /** 播放音效 */
     private playEffectMisic(adoName: string): void {
        let audioName = this.audioSrc.getAudioName(adoName);
        this.audioSrc.onPlayOneShot(audioName);
    }

     /** 播放音效 */
     private playBgMisic(adoName: string): void {
        let audioName = this.audioSrc.getAudioName(adoName);
        this.audioSrc.onPlayBgm(audioName)
    }


    public getMatchRanking(){
        this.wsSrc.getMatchRanking();
    }
    public showMatchRanking(ranking :RankingResult[]){
        this.show(this.matchRankingNode);
        this.matchRanking.updateGameList(ranking);
    }

    public onClickShowSettings(){
        this. audioSrc.clickEffect();
       this.audioSrc.onClickShowSettings();
    }

    public onClickShowGameDesc(){
        this. audioSrc.clickEffect();
        this.show(this.gameDesc);
       }
    public onClickHideGameDesc(){
        this. audioSrc.clickEffect();
        this.hide(this.gameDesc);
        const isFristClose=  sys.localStorage.getItem(this.firstName);
        if (isFristClose==null) {
            sys.localStorage.setItem(this.firstName, true); 
            console.log('isFristClose',isFristClose);
        }
    }

    public onClickShowGameList(){
        this. audioSrc.clickEffect();
        this.show(this.gameListNode);
        this.gameList.updateGameList();
       
    }
    public onClickHideGameList(){
        this. audioSrc.clickEffect();
        this.hide(this.gameListNode);
    }

    public closeRanking(){
        this.hide(this.RankingNode);
    }
    public showRanking(){
        this. audioSrc.clickEffect();
        this.show(this.RankingNode);
    }   

    public updateMatchRanking(ranking :RankingResult){
        // let isShow=this.wsSrc.getLoginInfo().viewingTableId=="1";
        // this.rankingLable.node.active=isShow;
        // this.playingLable.node.active=isShow;
        // this.RankingNode.parent.active=isShow;
        //更新自己
        if (ranking!=null) {
            this.rankingLable.string=ranking.rankingId+"/1000";
            this.playingLable.string=ranking.match_limit+"/"+ranking.match_limit_config;
          
        }
        
       }
    
    public UIMessage(sub: number, cmd) {
        switch (sub) {
            case NetDefine.GAME_C_SUB_BET_RETURN:
                let  result: GameResult; 
                result=cmd;
                if (result!=null) {
                    
                    console.log('order return=', result);
                    let randomArray=result.randomArray;
                    let total=randomArray[0]+randomArray[1]+randomArray[2];
                    this.diceCupController.setTotalScore(result.score);
                    let bonusScore=result.prize-total;
                    // 生成骰子点数
                    this.diceCupController.generateRandomResults(result.randomArray,bonusScore);
                    setTimeout(() => {
                        if (result.item!=0) {
                            this.svgaCtrl.triggerSecondAnimation(); 
                        }
                        this.svgaCtrl.scoreBgPlay();
                        console.log('stop loop='+0);
                    }, 2000);

    
                }
                break;

            case NetDefine. GAME_C_SUB_MATCH_RANKING_RETURN:
                 console.log('GAME_C_SUB_MATCH_RANKING_RETURN=', cmd);
                 this.showMatchRanking(cmd);
                 break
            case NetDefine. GAME_C_SUB_MATCH_RANKING_SELF_RETURN:
                console.log('GAME_C_SUB_MATCH_RANKING_SELF_RETURN=', cmd);  
                this.updateMatchRanking(cmd); 
                break     
            default:
                break;
        }
    }
   // 退出游戏
   exitGame() {
    if (typeof window !== 'undefined' && window.close) {
        window.close();
    } else {
        console.log('网页环境下无法直接关闭窗口');
        // 可以显示退出提示
        this.showExitMessage();
    }
    }

   private showExitMessage() {
    alert('请手动关闭浏览器标签页');
 }

}




